package com.situ.controller;

import com.alipay.api.internal.util.AlipaySignature;
import com.situ.bean.Order;
import com.situ.bean.OrderResponse;
import com.situ.dao.OrderDao;
import com.situ.service.impl.OrderService;
import com.situ.util.LayData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/orders")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/my-orders")
    public ResponseEntity<OrderResponse> getMyOrders(@RequestParam int userid) {
        System.out.println("查询订单");
        OrderResponse response = orderService.getOrdersWithDetailsByUserId(userid);
        System.out.println(response);
        return ResponseEntity.ok(response);
    }

   @PostMapping("/paysuccess")
   @ResponseBody
    public LayData handleAlipayNotify(HttpSession session, @RequestParam HashMap map) {// 获取支付宝POST过来的参数
       System.out.println("支付成功");
     String code=(String)  map.get("out_trade_no");
       Order order=new Order();
       order.setCode(code);
     System.out.println("支付成功"+code);
       orderService.paymentsuccess(order);
       LayData data=new LayData();
        session.setAttribute("paid",1);
        System.out.println();
       return   data;
    }


    @PostMapping ("/check-payment-status")
    @ResponseBody
    public LayData checkPaymentStatus(@RequestBody Order order) {
        System.out.println("查询订单支付状态");
         System.out.println(order.toString());

        int paid;
        System.out.println(orderService.getpayorder(order).getState());
        if (orderService.getpayorder(order).getState().equals("已支付")) {
           paid=1;
          System.out.println("订单已支付");
        } else {
            paid = 0;
        }


        LayData data = new LayData();
        if (paid == 1) {
            data.setCode(0);
            data.setMsg("订单已支付");
            System.out.println("订单已支付");
            data.setPaid(paid);

        } else {
            data.setCode(1);
            data.setMsg("订单未支付");
            data.setPaid(0);
        }

        return data;
    }


    @GetMapping
    public List<Order> getAllOrders() {
        System.out.println("查询所有订单");
        return orderService.getAllOrders();
    }



    // 根据ID获取订单
    @GetMapping("/{id}")
    public Order getOrderById(@PathVariable int id) {
        return orderService.getOrderById(id);
    }



    @GetMapping("/search")
    public Order getOrderByCode(@RequestParam(required = false) String code) {
        System.out.println("CODE查询订单");
        Order order = new Order();
        order.setCode(code);
        return orderService.getpayorder(order); // 返回单个订单
    }







    // 更新订单状态
    @PutMapping("/{id}")
    public String updateOrderStatus(@PathVariable int id, @RequestBody Order order) {
        order.setId(id);
        System.out.println("更新订单状态");
        System.out.println(order.toString());
        orderService.updateOrders(order);
        return "订单状态更新成功";
    }

    // 删除订单
    @DeleteMapping("/{id}")
    public String deleteOrder(@PathVariable int id) {
        orderService.deleteOrder(id);
        return "订单删除成功";
    }










}